﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Windows.Interop;

using Stanza.Silverlight.Browser;

namespace Stanza.Silverlight.Controls
{
    public class AutoSizeUserControl : UserControl
    {
        private const double DefaultExtraWidth = 0.0;
        private const double DefaultExtraHeight = 0.0;

        public static readonly DependencyProperty ExtraWidthProperty =
            DependencyProperty.Register( "ExtraWidth", typeof( Double ), typeof( AutoSizeUserControl ), new PropertyMetadata( DefaultExtraWidth ) );
        public static readonly DependencyProperty ExtraHeightProperty =
            DependencyProperty.Register( "ExtraHeight", typeof( Double ), typeof( AutoSizeUserControl ), new PropertyMetadata( DefaultExtraHeight ) );

        public double ExtraWidth
        {
            get { return (double)GetValue( ExtraWidthProperty ); }
            set { SetValue( ExtraWidthProperty, value ); }
        }

        public double ExtraHeight
        {
            get { return (double)GetValue( ExtraHeightProperty ); }
            set { SetValue( ExtraHeightProperty, value ); }
        }

        public AutoSizeUserControl()
        {
            if( !DesignerTools.IsInDesignMode )
            {
                Loaded += new RoutedEventHandler( AutoSizeUserControl_Loaded );
                Application.Current.Host.Content.FullScreenChanged += new EventHandler( Content_FullScreenChanged );
            }
        }

        double AdjustRange( double value, double min, double max )
        {
            if( min > 0 )
                if( value < min )
                    value = min;
            if( max > min )
                if( value > max )
                    value = max;
            if( value < 1 )
                value = 1;
            return value;
        }

        void AdjustControlSize()
        {
            Content c = Application.Current.Host.Content;

            Size sz = new Size();
            if( c.IsFullScreen )
            {
                sz.Width = c.ActualWidth;
                sz.Height = c.ActualHeight;
            }
            else
            {
                sz.Width = AdjustRange( HtmlTools.BrowserWindowWidth - ExtraWidth, MinWidth, MaxWidth );
                sz.Height = AdjustRange( HtmlTools.BrowserWindowHeight - ExtraHeight, MinHeight, MaxHeight );
            }

            if( !c.IsFullScreen )
            {
                HtmlPage.Plugin.SetStyleAttribute( "width", sz.Width.ToString( "F0" ) );
                HtmlPage.Plugin.SetStyleAttribute( "height", sz.Height.ToString( "F0" ) );
            }

            FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
            if( root != null )
            {
                root.Width = sz.Width;
                root.Height = sz.Height;
            }
        }

        void AutoSizeUserControl_Loaded( object sender, RoutedEventArgs e )
        {
            AdjustControlSize();
            HtmlPage.Window.AttachEvent( "onresize", new EventHandler<HtmlEventArgs>( BrowserWindow_Resized ) );
        }

        void BrowserWindow_Resized( object sender, HtmlEventArgs e )
        {
            AdjustControlSize();
        }

        void Content_FullScreenChanged( object sender, EventArgs e )
        {
            AdjustControlSize();
        }
    }
}
